package com.zy.mc_module.ui.micang.setting;

import android.content.Intent;

import androidx.annotation.NonNull;

import com.hwangjr.rxbus.RxBus;
import com.zy.mc_module.R;
import com.zy.mc_module.base.kotlin.BaseActivity;
import com.zy.mc_module.common.Constant;
import com.zy.mc_module.common.RxBusCode;
import com.zy.mc_module.databinding.ActivityMicangSettingMessBinding;
import com.zy.mc_module.ui.micang.RiceContract;
import com.zy.mc_module.ui.micang.RicePresenter;
import com.zy.mc_module.utils.AliOssUtils;
import com.zy.mc_module.utils.CameraUtils;
import com.zy.mc_module.utils.GlideUtil;
import com.zy.mc_module.utils.Logs;
import com.zy.mc_module.utils.NoDoubleUtil;
import com.zy.mc_module.utils.StatusBarUtil;
import com.zy.mc_module.utils.ToastUtil;
import com.zy.mc_module.wheel.dialog.CommonDialog;

import java.util.Arrays;
import java.util.List;

/**
 * created by Jaygee_vi
 * 2022/10/20
 */
public class MiCangSettingMessActivity extends
        BaseActivity<RiceContract.View , RicePresenter , ActivityMicangSettingMessBinding> {
    @NonNull
    @Override
    public ActivityMicangSettingMessBinding getViewBinding() {
        return ActivityMicangSettingMessBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        super.initView();
        StatusBarUtil.setImmersiveStatusBar(this);
        binding.viewTitle.tvTitle.setText("个人信息设置");
    }

    @Override
    protected void initListener() {
        super.initListener();
        binding.avatarCl.setOnClickListener(v -> {
            if (NoDoubleUtil.isFastClick()) {
                new CommonDialog(context, Constant.selectImg, position -> {
                    if (position == 0) {
                        //拍照
                        CameraUtils.openCameraImage(this);
                    } else if (position == 1) {
                        //从相册选取
                        CameraUtils.openLocalImage(this);
                    }
                }).show();
            }
        });
    }

    /***
     * 拍照/相册回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            //拍照
            case CameraUtils.GET_IMAGE_BY_CAMERA:
                if (resultCode == RESULT_OK && null != CameraUtils.imageUriFromCamera) {
                    // 对图片进行裁剪
                    CameraUtils.cropImage(this, CameraUtils.imageUriFromCamera);
                }
                break;

            //相册
            case CameraUtils.GET_IMAGE_BY_PHOTO:
                if (resultCode == RESULT_OK && null != data.getData()) {
                    // 对图片进行裁剪
                    CameraUtils.cropImage(this, data.getData());
                }
                break;

            // 裁剪图片后结果
            case CameraUtils.CROP_IMAGE:
                if (resultCode == RESULT_OK & null != CameraUtils.imageCrop) {
                    //显示裁剪后的图片
                    GlideUtil.displayImage(binding.avatar, CameraUtils.imageCrop, R.mipmap.user_default_head);
                    compressImg(Arrays.asList(CameraUtils.imageCrop));
                }
                break;
        }
    }

    /***
     * 压缩图片
     * @param paths
     */
    public void compressImg(List<String> paths) {
        AliOssUtils.getInstance().init(this);
        AliOssUtils.getInstance().uploadFile(new AliOssUtils.UploadFileListener() {
            @Override
            public void uploadProgress(String path, long currentSize, long totalSize) {

            }

            @Override
            public void uploadSuccess(String url) {
                Logs.Log_e("czyyy",url);
            }

            @Override
            public void uploadFailed(String message) {
                Logs.Log_e("czyyy",message);
            }
        },paths.get(0));
    }

}
